package com.eric.rabbit.config;

import com.eric.rabbit.entity.fanout.FanoutReceiver;
import com.eric.rabbit.entity.fanout.FanoutSender;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author:Eric
 * @date:2021/6/28
 * 发布/订阅模式
 * 一个生产者 -> 一个exchange -> 两个队列Queue -> 各自对应的消费者
 * 各自消费各自的消息
 */
@Configuration
public class FanoutRabbitConfig {

    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanout.exchange");
    }

    @Bean
    public Queue fanoutQueue1(){
        //代表一个匿名的、非持久的、独占的、自动删除的队列
        return new AnonymousQueue();
    }

    @Bean
    public Queue fanoutQueue2(){
        //代表一个匿名的、非持久的、独占的、自动删除的队列
        return new AnonymousQueue();
    }

    @Bean
    public Binding fanoutBind1(){
        //进行exchange与queue绑定
        return BindingBuilder.bind(fanoutQueue1()).to(fanoutExchange());
    }

    @Bean
    public Binding fanoutBind2(){
        //进行exchange与queue绑定
        return BindingBuilder.bind(fanoutQueue2()).to(fanoutExchange());
    }

    @Bean
    public FanoutSender fanoutSender(){
        return new FanoutSender();
    }

    @Bean
    public FanoutReceiver fanoutReceiver(){
        return new FanoutReceiver();
    }

}
